"""
Convergence Analysis

Tools for analyzing solver convergence behavior and performance.
"""

"""
    convergence_analysis(result::LidDrivenResult) -> NamedTuple

Analyze convergence behavior from solver result.
"""
function convergence_analysis(result::LidDrivenResult)
    if isempty(result.convergence_history)
        @warn "No convergence history available"
        return (analysis_available = false,)
    end
    
    history = result.convergence_history
    n_steps = length(history)
    
    # Basic statistics
    final_residual = history[end]
    initial_residual = history[1]
    reduction_factor = initial_residual / final_residual
    
    # Estimate convergence rate (geometric mean)
    if n_steps > 10 && final_residual > 0
        log_reduction = log(final_residual / initial_residual)
        avg_rate = exp(log_reduction / (n_steps - 1))
    else
        avg_rate = NaN
    end
    
    return (
        analysis_available = true,
        initial_residual = initial_residual,
        final_residual = final_residual,
        reduction_factor = reduction_factor,
        convergence_rate = avg_rate,
        iterations = n_steps,
        converged = result.converged
    )
end